package com.huishu.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huishu.commons.pojo.Result;
import com.huishu.commons.utlis.BeanUtils;
import com.huishu.user.mapper.MenuMapper;
import com.huishu.user.mapper.UserRoleMapper;
import com.huishu.user.pojo.dto.PageListDTO;
import com.huishu.user.pojo.model.MenuDO;
import com.huishu.user.pojo.model.UserRoleDO;
import com.huishu.user.pojo.vo.MenuVO;
import com.huishu.user.service.MenuService;
import com.huishu.user.utils.RecursiveUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 菜单表 服务实现类
 * </p>
 *
 * @author generator@ljj
 * @since 2023-04-21
 */
@Slf4j
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, MenuDO> implements MenuService {

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Override
    public List<MenuVO> getMenuList(Long id) {
        LambdaQueryWrapper<UserRoleDO> queryWrapper = Wrappers.lambdaQuery(UserRoleDO.class)
                .eq(UserRoleDO::getUserId, id);
        List<UserRoleDO> userRoleDOS = userRoleMapper.selectList(queryWrapper);
        List<Long> roleList = userRoleDOS.stream().map(UserRoleDO::getRoleId).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(roleList)) {
            log.info("用户没有设置角色:userId:{}", id);
            return new ArrayList<>();
        }
        return RecursiveUtils.getMenu(baseMapper.getMenuList(roleList));
    }

    @Override
    public Result add(MenuDO menuDO) {
        int insert = this.baseMapper.insert(menuDO);
        if (insert > 0) {
            return Result.success();
        }
        return Result.fail("新增失败");
    }

    @Override
    public Result modify(MenuDO menuDO) {
        int insert = this.baseMapper.updateById(menuDO);
        if (insert > 0) {
            return Result.success();
        }
        return Result.fail("修改失败");
    }

    @Override
    public Result pageList(PageListDTO pageListDTO) {
        Page<MenuDO> page = new Page<>(pageListDTO.getCurrPage(), pageListDTO.getPageSize());
        LambdaQueryWrapper<MenuDO> queryWrapper =Wrappers.lambdaQuery(MenuDO.class)
                .eq(StringUtils.isNotEmpty(pageListDTO.getSearch()),MenuDO::getMenuName,pageListDTO.getSearch());
        return Result.success(this.baseMapper.selectPage(page,queryWrapper));
    }

    @Override
    public Result findAll() {
        List<MenuDO> list = this.list();
        List<MenuVO> menuVOS = list.stream().map(menuDO -> BeanUtils.copyProperties(menuDO,MenuVO.class)).collect(Collectors.toList());
        return Result.success(RecursiveUtils.getMenu(menuVOS));
    }
}
